using UnityEngine;

/// <summary>
/// 2D 玩家移动控制器
/// 实现：左右移动 + 加速跑 + 跳跃 + 短跳 + 快速下落 + 自动朝向翻转
/// </summary>
[RequireComponent(typeof(Rigidbody2D))]
public class PlayerMovement2D_20250421 : MonoBehaviour
{
    [Header("移动参数")]
    public float moveSpeed = 5f; // 基础移动速度

    [Header("加速跑参数")]
    public float sprintMultiplier = 1.5f;  // 加速时的速度倍率
    public KeyCode sprintKey = KeyCode.LeftShift;  // 加速按键

    [Header("跳跃参数")]
    public float jumpForce = 6f; // 跳跃力
    public Transform groundCheck; // 地面检测点
    public float groundCheckRadius = 0.2f; // 检测半径
    public LayerMask groundLayer; // 设定为地面的 Layer

    [Header("跳跃增强参数")]
    public float fallMultiplier = 2.5f; // 下落加重力倍率
    public float lowJumpMultiplier = 2f; // 松开跳跃键后的跳跃缩减倍率

    [Header("翻转角色")]
    public bool flipSprite = true; // 是否启用朝向翻转

    private Rigidbody2D rb;
    private bool isGrounded; // 是否在地面上
    private bool jumpPressed = false; // 是否按下跳跃
    private float horizontalInput; // 水平方向输入
    private Vector3 originalScale; // 初始朝向缩放值

    private void Awake()
    {
        rb = GetComponent<Rigidbody2D>();
        originalScale = transform.localScale; // 记录初始朝向
    }

    private void Update()
    {
        // ✅ 检测水平移动输入（-1, 0, 1）
        horizontalInput = Input.GetAxisRaw("Horizontal");

        // ✅ 记录跳跃意图（只记录一次）
        if (Input.GetButtonDown("Jump") && isGrounded)
        {
            jumpPressed = true;
        }

        // ✅ 翻转角色朝向
        if (flipSprite && horizontalInput != 0)
        {
            Vector3 scale = originalScale;
            scale.x = horizontalInput > 0 ? Mathf.Abs(scale.x) : -Mathf.Abs(scale.x);
            transform.localScale = scale;
        }
    }

    private void FixedUpdate()
    {
        // ✅ 地面检测放前面，确保用于跳跃判断
        isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, groundLayer);

        // ✅ 计算是否加速
        float currentSpeed = moveSpeed;
        if (Input.GetKey(sprintKey))
        {
            currentSpeed *= sprintMultiplier;
        }

        // ✅ 移动处理
        rb.velocity = new Vector2(horizontalInput * currentSpeed, rb.velocity.y);

        // ✅ 执行跳跃
        if (jumpPressed)
        {
            rb.velocity = new Vector2(rb.velocity.x, jumpForce);
            jumpPressed = false; // 立即重置跳跃意图
        }

        // ✅ 增强跳跃体验
        if (rb.velocity.y < 0)
        {
            // 下落阶段加快重力，加快下落速度（像马里奥那样不飘）
            rb.velocity += Vector2.up * Physics2D.gravity.y * (fallMultiplier - 1) * Time.fixedDeltaTime;
        }
        else if (rb.velocity.y > 0 && !Input.GetButton("Jump"))
        {
            // 上升阶段如果松开跳跃键，就不让跳太高（实现短跳）
            rb.velocity += Vector2.up * Physics2D.gravity.y * (lowJumpMultiplier - 1) * Time.fixedDeltaTime;
        }
    }

    // ✅ 可视化地面检测区域
    private void OnDrawGizmosSelected()
    {
        if (groundCheck != null)
        {
            Gizmos.color = Color.green;
            Gizmos.DrawWireSphere(groundCheck.position, groundCheckRadius);
        }
    }
}
